package com.example.myprojectoffeixue;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.java_websocket.client.WebSocketClient;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ASUS on 2017/2/16.
 */
public class adapter_HouseDevice extends BaseAdapter {
    private ArrayList<SheBeiClass> d;
    String cmd;
    String j;
    private String s;
    private MyApplication m;
    private Context context;
    private LayoutInflater layoutInflater;
    private TextView bt;
    private TextView bt1;
    private TextView bt2;
    WebSocketClient wsc;
    private HouseDeviceActivity h;
    private String ieee;
    private String port;
    private String mac;

    public adapter_HouseDevice(Context context) {
        super();
        this.context = context;
        h= (HouseDeviceActivity) context;
        m= (MyApplication) h.getApplication();
        this.d = m.hu.devices;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        return d.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder h = null;

        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_list, null);
            h.tv = (TextView) convertView.findViewById(R.id.tv_listview);
            h.iv = (ImageView) convertView.findViewById(R.id.iv_listview);
            h.iv1 = (ImageView) convertView.findViewById(R.id.iv1_listview);
            h.l = (LinearLayout) convertView.findViewById(R.id.bt_lin);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }


            h.tv.setText(d.get(position).deviceName);

        if (d.get(position).deviceId != null) {

            if ("0100".equals(d.get(position).deviceId)) {
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                //墙面三路开关
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.kai);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.guan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }


            } else if ("010B".equals(d.get(position).deviceId)||"010A".equals(d.get(position).deviceId)) {
                //智能门锁
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.ms);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.msguan);
                    h.iv1.setImageResource(R.drawable.ankai);

                }


            } else if ("010C".equals(d.get(position).deviceId)) {
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
//取电
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudiana);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.qudianguan);
                    h.iv1.setImageResource(R.drawable.ankai);
                }

            } else if ("0002".equals(d.get(position).deviceId)) {
                //窗帘
                h.l.setVisibility(View.VISIBLE);
                h.iv1.setVisibility(View.GONE);
                h.iv.setImageResource(R.drawable.cl2);
                // h.l1.setVisibility(View.GONE);
                if ("01".equals(d.get(position).status)) {
                    h.l.setBackgroundResource(R.drawable.clk);
                } else if ("00".equals(d.get(position).status)) {
                    h.l.setBackgroundResource(R.drawable.clg);
                } else if ("02".equals(d.get(position).status)) {
                    h.l.setBackgroundResource(R.drawable.clt);

                }
            } else if ("010F".equals(d.get(position).deviceId)) {

                h.iv.setImageResource(R.drawable.zykt);
                h.iv1.setVisibility(View.GONE);
                h.l.setVisibility(View.GONE);
                //中央空调


            }else if("0009".equals(d.get(position).deviceId)){
                //插座
                h.l.setVisibility(View.GONE);
                h.iv1.setVisibility(View.VISIBLE);
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.chazuokai);
                    h.iv1.setImageResource(R.drawable.anguan);

                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.chazuo);
                    h.iv1.setImageResource(R.drawable.ankai);
                }





            }else if("0020".equals(d.get(position).deviceId)){
                h.iv.setImageResource(R.drawable.hwzfq);
                h.iv1.setVisibility(View.GONE);
                h.l.setVisibility(View.GONE);


            }else if("010E".equals(d.get(position).deviceId)) {
//红外
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw2);
                    h.iv1.setImageResource(R.drawable.anguan);
                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw);
                    h.iv1.setImageResource(R.drawable.ankai);

                } else if ("04".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.hw2);
                    h.iv1.setImageResource(R.drawable.anguan);
                }

            }else if ("010D".equals(d.get(position).deviceId)) {
//门磁
                if ("01".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.mc);
                    h.iv1.setImageResource(R.drawable.anguan);
                } else if ("00".equals(d.get(position).status)) {
                    h.iv.setImageResource(R.drawable.mcguan);
                    h.iv1.setImageResource(R.drawable.ankai);
                }


            }
        }


        h.iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("0100".equals(d.get(position).deviceId)){
                    cmd="1007";
                }else if("010B".equals(d.get(position).deviceId)||"010A".equals(d.get(position).deviceId)){
                    cmd="1005";
                }else if("010C".equals(d.get(position).deviceId)){
                    cmd="1006";
                }else if("0002".equals(d.get(position).deviceId)){
                    cmd="1008";
                }else if("010E".equals(d.get(position).deviceId)){
                    cmd="1010";
                }else{
                    return;
                }
                    wsc = m.c;
                    if (d.size() != 0) {
                        s = d.get(position).status;
                        ieee = d.get(position).ieee;
                        port = d.get(position).port;
                        mac = d.get(position).mac;

                        if ("00".equals(s)) {
                            s = "01";
                        } else {
                            s = "00";
                        }
                        Kuaiguanutil k = new Kuaiguanutil();
                        k.cmd = cmd;
                        k.ieee = ieee;
                        k.port = port;
                        k.status = s;
                        k.mac = mac;
                        String j = JSON.toJSONString(k);
                        //wsc.sendTextMessage(j);
                        if (m.flag3) {
                            wsc.send(j);
                        }else{
                            ToastUtils.showToast(context,"与后台服务器连接断开，正在重连");
                        }

                    }
                }

        });


        bt = (TextView) h.l.findViewById(R.id.bt_one);
        bt1 = (TextView) h.l.findViewById(R.id.bt_two);
        bt2 = (TextView) h.l.findViewById(R.id.bt_three);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {
                    s = "01";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.status=s;
                    k.cmd="1008";
                    k.mac=d.get(position).mac;
                    j = JSON.toJSONString(k);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "01";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(111);
                    }
                }
            }
        });

        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wsc = m.c;
                if (d.size() != 0) {
                    s = "02";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd="1008";
                    k.status=s;
                    k.mac=d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd",j);
                    if (m.flag3) {
                        wsc.send(j);
                    }

                    d.get(position).status = "02";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(111);
                    }

                }
            }
        });

        bt2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                wsc = m.c;
                if (d.size() != 0) {
                    s = "00";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.ieee = d.get(position).ieee;
                    k.port = d.get(position).port;
                    k.cmd="1008";
                    k.status=s;
                    k.mac=d.get(position).mac;
                    j = JSON.toJSONString(k);
                    Log.d("cmd",j);
                    if (m.flag3) {
                        wsc.send(j);
                    }
                    d.get(position).status = "00";
                    if (m.hand != null) {
                        m.hand.sendEmptyMessage(111);
                    }

                }
            }
        });
        return convertView;
    }

    class ViewHolder {

        TextView tv;
        ImageView iv;
        ImageView iv1;
        LinearLayout l;

    }

}